//
//  CartProdItemView.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/13.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "CartProdItemView.h"
#import "CartProdSub.h"
#import "CalcView.h"
#import "CartPresentView.h"
#import "CartSalesWithCountDownView.h"

@interface CartProdItemView()

/** 商品图片 */
@property (nonatomic, weak) UIImageView *iconView;
/** 商品名称 */
@property (nonatomic, weak) UILabel *nameLabel;
/** 订购链接 */
@property (nonatomic, weak) UILabel *skuLabel;
/** 价格 */
@property (nonatomic, weak) UILabel *priceLabel;
/**赠品*/
@property(nonatomic,weak) CartPresentView *presentView;
/**抢购价*/
@property(nonatomic,weak) CartSalesWithCountDownView *salesView;

@end

@implementation CartProdItemView
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) { // 初始化子控件
        //1.商品图片
        UIImageView *iconView = [[UIImageView alloc] init];
        [iconView setUserInteractionEnabled:YES];
        //设置layer
        CALayer *layer=[iconView layer];
        //设置边框线的宽
        [layer setBorderWidth:0.5];
        //设置边框线的颜色
        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
        self.iconView = iconView;
        UITapGestureRecognizer *iconViewtapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconClick)];
        [self.iconView addGestureRecognizer:iconViewtapGesture];
        [self addSubview:iconView];
        
        //2.商品名称
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.textColor = [UIColor blackColor];
        [nameLabel setFont:[UIFont systemFontOfSize:PRODUCT_NAME_SIZE]];
        nameLabel.numberOfLines = 2;
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        //3.订购链接
        UILabel *skuLabel = [[UILabel alloc] init];
        skuLabel.textColor = JXColor(77, 77, 77);
        [skuLabel setFont:[UIFont systemFontOfSize:PRODUCT_SKU_SIZE]];
        skuLabel.numberOfLines = 1;
        [self addSubview:skuLabel];
        self.skuLabel = skuLabel;
        
        //4.价格
        UILabel *priceLabel = [[UILabel alloc] init];
        priceLabel.textColor = JXThemeColor;
        [priceLabel setFont:[UIFont systemFontOfSize:PRODUCT_PRICE_SIZE]];
        [self addSubview:priceLabel];
        self.priceLabel = priceLabel;
        
        //5.计算器
        CalcView *calcView = [[CalcView alloc]initWithWidth:[DimensUtils getScaleX:99] Height:[DimensUtils getScaleX:23] addImage:@"ic_calc_add_cart" minusImage:@"ic_calc_minus_cart"];
        [calcView.numLabbel.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
        calcView.x = JXScreenW-JXPublicInset-calcView.width;
        [self addSubview:calcView];
        self.calcView = calcView;
        
        //6.赠品
        CartPresentView *presentView = [[CartPresentView alloc]init];
        [self addSubview:presentView];
        [presentView setHidden:YES];
        self.presentView = presentView;
        
        //7.抢购价
        CartSalesWithCountDownView *salesView = [[CartSalesWithCountDownView alloc]init];
        [self addSubview:salesView];
        [salesView setHidden:YES];
        self.salesView = salesView;
        
        // 8.cell的设置
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}

-(void)setCartProdItemFrame:(CartProdItemFrame *)cartProdItemFrame{
    _cartProdItemFrame = cartProdItemFrame;
    self.frame = cartProdItemFrame.frame;
    CartProdSub *infos = cartProdItemFrame.infos;
    
    
    //1.商品图片
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:infos.prodImageUrl]];
    self.iconView.frame =cartProdItemFrame.iconFrame;
    
    //2.商品名称
    self.nameLabel.frame = cartProdItemFrame.nameFrame;
    self.nameLabel.text = infos.prodName;
    
    //3.订购链接
    self.skuLabel.text = infos.prodBuyLink;
    self.skuLabel.frame = cartProdItemFrame.skuFrame;
    
    //4.价格
    NSString *dor = @"￥";
    dor = [dor stringByAppendingString:infos.prodNewPrice];
    self.priceLabel.text = dor;
    self.priceLabel.frame = cartProdItemFrame.priceFrame;
    
    //5.计算器
    NSString *prodCount = infos.prodCount;
    [self.calcView setNum:prodCount.integerValue];
    self.calcView.y = (int)cartProdItemFrame.calcY;
    
    //6.赠品和限时抢购
    ProdPromotion *prodPromotion =  infos.prodPromotion;
    if(prodPromotion){
        if(prodPromotion.presentList.count>0){//判断赠品
            [self.presentView setHidden:NO];
            self.presentView.x  = cartProdItemFrame.presentFrameX;
            self.presentView.y  = cartProdItemFrame.presentFrameY;
            [self.presentView setPresentDesc:[prodPromotion.presentList[0] name]];
        }else{
            [self.presentView setHidden:YES];
        }
        if(prodPromotion.price){//判断抢购
            [self.salesView setHidden:NO];
            if(prodPromotion.presentList.count>0){
                self.salesView.x = cartProdItemFrame.salesFrameX;
                self.salesView.y = cartProdItemFrame.salesFrameY;
                [self.salesView setPrice:prodPromotion.price];
                [self.salesView setTime:prodPromotion.endTime];
            }else{
                self.salesView.x = cartProdItemFrame.salesFrameX;
                self.salesView.y = cartProdItemFrame.salesFrameY;
                [self.salesView setPrice:prodPromotion.price];
                [self.salesView setTime:prodPromotion.endTime];
            }
        }else{
            [self.salesView setHidden:YES];
        }
    }else{
         [self.presentView setHidden:YES];
         [self.salesView setHidden:YES];
    }
    
}

-(void)setTag:(NSInteger)tag{
    [super setTag:tag];
    [self.calcView setTag:tag];
}

//点击图片
-(void)iconClick{
    if ([self.delegate respondsToSelector:@selector(toProductDetail:)]) {
        [_delegate toProductDetail:self.tag];
    }
}

@end
